package com.hl.card.mq;

import com.hl.card.common.mq.RabbitConfigBase;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
@Service
public class RabbitConfig {
    @Resource
    private Environment env;

    @Autowired
    private RabbitConfigBase rabbitConfigBase;

    @Bean
    public AmqpTemplate amqpTemplate() {
        return rabbitConfigBase.amqpTemplate();
    }

    //创建mq连接
    @Bean("connectionFactory")
    public ConnectionFactory connectionFactory() {
        return rabbitConfigBase.connectionFactory();
    }

    /**
     * 初始化消息队列
     * @param
     * -Map-
     * <key>: 消息队列名称
     * <value>=: 是否对该队列启用延时消息功能
     * @return boolean
     */
    @Bean
    public boolean init() throws IOException {
        Map<String, Boolean> queue_config = new HashMap<>();
//        queue_config.put(MqCustomConfig.PAY_NOTIFY_QUEUE, MqCustomConfig.ENABLE_PAY_NOTIFY_QUEUE_DELAY_MESSAGE);
//        queue_config.put(MqCustomConfig.QUEUE_COMMON_TEAT, MqCustomConfig.ENABLE_QUEUE_COMMON_TEAT_DELAY_MESSAGE);
//        queue_config.put(MqCustomConfig.QUEUE_AWARD, MqCustomConfig.ENABLE_QUEUE_AWARD_DELAY_MESSAGE);
        //boolean autoConfig = Boolean.parseBoolean(env.getProperty("RabbitMQ.autoConfig"));
//        if(autoConfig) {
//            return false;
//        }
        return rabbitConfigBase.init(MqCustomConfig.QUEUE_DEFINE);
    }
}